﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using MySQL.Db;
using Gollie.Invoice;

namespace Gollie.Administration.Service
{
	class Queries
	{
		internal static void DeleteExpiredTransactions()
		{
			Query query = new Query("sp_deleteexpiredtransactions");

			query.Execute();
		}

		internal static void DeleteExpiredPendingPayments()
		{
			Query query = new Query("sp_deleteexpiredpendingpayments");

			query.Execute();
		}

		internal static void DeleteOldMails()
		{
			Query query = new Query("sp_deleteoldmails");

			query.Execute();
		}

		internal static void DeleteNotActivated()
		{
			Query query = new Query("sp_deletenotactivated");

			query.Execute();
		}

		internal static void CloseMonth(InvoiceCalculatorCollection invoiceCalculators, int month, int year)
		{
			QueryCollection queries = new QueryCollection();

			foreach (KeyValuePair<uint, InvoiceCalculator> kvp in invoiceCalculators)
			{
				QueryArguments args = new QueryArguments();

				args.Add("p_vendorid", kvp.Key);
				args.Add("p_month", month);
				args.Add("p_year", year);
				args.Add("p_totaldeposit", kvp.Value.TotalDeposit);
				args.Add("p_fee", kvp.Value.NetFee);
				args.Add("p_vat", kvp.Value.TotalVat);

				Query query = new Query("sp_insertmonthclose", args);

				queries.Add(query);
			}

			queries.Execute();
		}

		internal static string InsertInvoice(uint vendorId, int month, int year, string path, decimal amount, decimal vat)
		{
			QueryArguments args = new QueryArguments();

			args.Add("p_vendorid", vendorId);
			args.Add("p_month", month);
			args.Add("p_year", year);
			args.Add("p_path", path);
			args.Add("p_amount", amount);
			args.Add("p_vat", vat);

			Query query = new Query("sp_insertinvoice", args);
			string factNr = (string)query.Execute();

			return factNr;
		}

		internal static void DeleteInvoice(string factNr)
		{
			Query query = new Query("sp_deleteinvoice", "p_factnr", factNr);

			query.Execute();
				
		}

		internal static ResultSet GetVendor(uint vendorId)
		{
			Query query = new Query("sp_getvendor", "p_vendorid", vendorId);

			return (ResultSet)query.Execute();
		}
	}
}
